package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.mapper.MajorsMapper;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.entity.Majors;
import com.example.demo.entity.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class UpdateStuController {
    @Autowired
    private  StudentMapper sm;
    @Autowired
    private  MajorsMapper majorsMapper;
    @RequestMapping("/updatestu")
    public void updatestu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id=Integer.valueOf(request.getParameter("id"));
        String name=request.getParameter("name");
        String college=request.getParameter("college");
        String major=request.getParameter("major");
        String classi=request.getParameter("classi");
        String tele=request.getParameter("tele");
        if(!"".equals(id)&&!"".equals(name)&&!"".equals(college)&&!"".equals(major)&&!"".equals(classi)&&!"".equals(tele)&&name!=null&&college!=null&&major!=null&&classi!=null&&tele!=null) {
            Majors majors = majorsMapper.selectByCollege(college);
            if(majors==null) response.getWriter().write(JSONObject.toJSONString(Message.failure("Failure update,college,is not exist")));
            boolean isMajorExist = majors.getMajor().contains(major);
            int n = sm.updateStu(name, college, major, classi, tele, id);
            if (n > 0 && isMajorExist) {
                response.getWriter().write(JSONObject.toJSONString(Message.success("Success update")));
            } else response.getWriter().write(JSONObject.toJSONString(Message.failure("Failure update")));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("Failure update,data exist null")));
    }
}
